/*
        libidxp listener example:
                This is a simple example of creating a libidxp listener using
                libidxp library. It shows how to use some of the features included
                in this version of the library.
*/

/* Library inclusion */
#include <libidxp.h>

/* Custom function called automatically by the library through the use of "idxp_custom" family functions (see below..) */
/* In this type of function you can handle the frame and its payload using idxp_frame_get_payload(frame) */
bool CUSTOM_INRECEIVE_FUNCTION(IdxpConnection *connection,IdxpChannel *channel,IdxpFrame *frame, axlPointer data){
        printf("Custom Business logic\n");
        return true;
}



int main(int argc, char **argv){
    /* Declaration of structures used by libidxp such as connection and channel pointers, frame pointer and so on .. */
	IdxpConnection *connection = NULL;


	/* Initialization MACRO for listener side, it must be present before any other libidxp function call!!! */
	IDXPLIB_LISTENER;


	/* Idxp custom family function telling the library to make a call to
           CUSTOM_INRECEIVE_FUNCTION everytime a message is received and the
           channel role is HEARTBEAT/CLIENT                                  */
	idxp_custom_heartbeat_client_set(CUSTOM_INRECEIVE_FUNCTION);
	

        /* if at least one argument is passed to the program, that argument will be used as configuration file path
        and passed to idxp_configuration_load() */
        (argc>1) ? idxp_configuration_load(argv[1],NULL) : idxp_configuration_load("Insert here the default configuration file path",NULL);
	

	/* Starting to listen to loopback interface (127.0.0.1) port 44441 */
	/* NOTE: You can call this function one or more times in order to listen to one or more ports/interfaces */
	/* 	 However, you have to make these calls BEFORE calling idxp_listener_wait()			 */
	connection = idxp_connection_new(connection,"127.0.0.1","44441");


	/* The following function blocks the listener main before the end of the program */
	idxp_listener_wait();

	 /* This function MUST be called in order to close the program properly and deallocate libidxp environment */
	idxp_exit();


	return 1;
}
